package cn.terminus.service.customer.feign;

import cn.terminus.result.common.constant.DefaultParameterConst;
import cn.terminus.result.common.context.TerminusCallContext;
import cn.terminus.result.common.context.builder.TerminusCallContextBuilder;
import cn.terminus.result.common.context.holder.TerminusSessionContextHolder;
import feign.RequestInterceptor;
import feign.RequestTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

@Component
public class FeignInterceptor implements RequestInterceptor {

    @Resource
    private TerminusSessionContextHolder terminusSessionContextHolder;

    @Override
    public void apply(RequestTemplate requestTemplate) {
        String url = requestTemplate.url();
        if (url != null && !(url.contains("/projectRoom/getDetailByIds"))) {

            TerminusCallContext callContext = getCallContext();
            requestTemplate.header(DefaultParameterConst.BUSINESS_ID, String.valueOf(callContext.getBusiness().getBusinessId()));
            requestTemplate.header(DefaultParameterConst.USER_ID, String.valueOf(callContext.getUser().getCaller()));
        }
    }

    private TerminusCallContext getCallContext() {
        return TerminusCallContextBuilder.buildContext(terminusSessionContextHolder);
    }
}
